﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using ByteSite.Common;

namespace ByteSite.BLL
{
    /// <summary>
    ///会员组别
    /// </summary>
    public partial class user_groups
    {
        private readonly Entitys.sysconfig sysConfig = new BLL.sysconfig().loadConfig();//获得系统配置信息
        private readonly DAL.Data data;

        public user_groups()
        {
            data = new DAL.Data();
        }

        #region 基本方法================================
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return data.Exists<Entitys.dt_user_groups>(id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Entitys.dt_user_groups model)
        {
            return data.Insert(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Entitys.dt_user_groups model)
        {
            return data.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            return data.Delete<Entitys.dt_user_groups>(id);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Entitys.dt_user_groups GetModel(int id)
        {
            return data.Get<Entitys.dt_user_groups>(id);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public List<Entitys.dt_user_groups> GetList(int Top, string strWhere, string filedOrder)
        {
            return data.List<Entitys.dt_user_groups>(Top, strWhere, filedOrder);
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public List<Entitys.dt_user_groups> GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            return data.List<Entitys.dt_user_groups>(pageSize, pageIndex, strWhere, filedOrder, out recordCount);
        }
        #endregion

        #region 扩展方法================================
        /// <summary>
        /// 返回用户组名称
        /// </summary>
        public string GetTitle(int id)
        {
            Entitys.dt_user_groups model = GetModel(id);
            if (model == null)
                return "";
            else
                return model.title;
        }

        /// <summary>
        /// 获取会员组折扣
        /// </summary>
        public int GetDiscount(int id)
        {

            return Utils.ObjToInt(GetModel(id).discount);
        }

        /// <summary>
        /// 取得默认组别实体
        /// </summary>
        public Entitys.dt_user_groups GetDefault()
        {
            return data.Get<Entitys.dt_user_groups>(x => x.id > 0, x => x.is_default, SqlSugar.OrderByType.Desc);
        }

        /// <summary>
        /// 根据经验值返回升级的组别实体
        /// </summary>
        public Entitys.dt_user_groups GetUpgrade(int group_id, int exp)
        {
            int grade = Utils.ObjToInt(data.Get<Entitys.dt_user_groups>(x => x.id == group_id && x.upgrade_exp <= exp).grade, 0);
            return data.Get<Entitys.dt_user_groups>(x => x.is_lock == 0 && x.is_upgrade == 1 && x.grade > grade, x => x.grade, SqlSugar.OrderByType.Asc);
        }
        #endregion
    }
}